package com.yang.newCustomersTransferBackProcess.service;

import com.yang.newCustomersTransferBackProcess.dao.RideRecordDao;
import com.yang.newCustomersTransferBackProcess.dao.SiteDao;
import com.yang.newCustomersTransferBackProcess.dao.UserDao;
import com.yang.newCustomersTransferBackProcess.pojo.RideRecord;
import com.yang.newCustomersTransferBackProcess.pojo.Site;
import com.yang.newCustomersTransferBackProcess.pojo.User;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.HashMap;
import java.util.Map;

@Service
public class RideRecordService {

    @Autowired
    private RideRecordDao rideRecordDao;

    @Autowired
    private UserDao userDao;

    @Autowired
    private SiteDao siteDao;

    @Transactional
    public Map<String, Object> byCar(int userId, int siteId) {

        User user = userDao.findById(userId).get();

        Site site = siteDao.findById(siteId).get();

        RideRecord rideRecord = rideRecordDao.queryNewestRideRecord(userId);
        if (rideRecord == null){
            RideRecord rideRecord1 = new RideRecord();
            rideRecord1.setUserId(userId);
            rideRecord1.setUserName(user.getName());
            rideRecord1.setArrivalId(siteId);
            rideRecord1.setArrivalName(site.getName());
            rideRecord1.setStatus(1);
            rideRecordDao.save(rideRecord1);
        }else {
            rideRecord.setStatus(2);
            rideRecord.setDepartureId(site.getId());
            rideRecord.setDepartureName(site.getName());
            rideRecordDao.save(rideRecord);

            user.setBalance(user.getBalance()-9);
            userDao.save(user);
        }

        Map<String, Object> msg = new HashMap<String, Object>();
        msg.put("massage", "扫码成功");
        msg.put("code", 200);
        return msg;
    }
}
